package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.Department;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "系部管理接口",value = "department")

@RestController
@RequestMapping("/Department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * author:wp
     * time:19:24 2018/5/31
     *
     * @param department
     * @return
     */
    @ApiOperation(value = "添加一个系部信息")
    @PostMapping("/add")
    public R addOne(@RequestBody Department department)
    {
       try{
           if(departmentService.findByName(department.getName())==null)
           {
               departmentService.addOne(department);
               return R.ok();
           }
           return R.error("该系已存在");

       }catch (Exception e){
           e.printStackTrace();
           return R.error();
       }
    }

    /**
     * author:wp
     * time:19:24 2018/5/31
     *
     * @param id
     * @return
     */

    @ApiOperation(value = "删除单个系部信息")
    @GetMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id)
    {
        try{
            departmentService.deleteById(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return  R.error();
        }
    }

    /**
     * author:wp
     * time:19:24 2018/5/31
     *
     * @param department
     * @return
     */
    @ApiOperation(value = "修改系部信息")
    @PostMapping("/update")
    public R update(@RequestBody Department department)
    {
        try{
            if(departmentService.findByName(department.getName())==null)
            {
                departmentService.update(department);
                return R.ok();
            }
            return R.error("系名重复");

        }catch (Exception  e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author:wp
     * time:19:24 2018/5/31
     * function:
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查找一个系部信息")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try {
            return R.ok().put("data",departmentService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author:wp
     * time:17:36 2018/6/2
     * function 返回一个学院的所有系部信息
     *
     * @param schoolid
     * @return
     */

    @ApiOperation(value="查找一个学院的所有系部信息")
    @GetMapping("/findBySchoolid/{schoolid}")
    public R findBySchoolid(@PathVariable Integer schoolid)
    {
        try{
            return R.ok().put("data",departmentService.findBySchoolid(schoolid));

        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
